package com.carboy.launch.activity.setting;

import java.util.ArrayList;
import java.util.List;

import android.app.AlertDialog;
import android.app.Dialog;
import android.content.Context;
import android.content.Intent;
import android.content.res.Configuration;
import android.media.AudioManager;
import android.os.Bundle;
import android.os.Parcelable;
import android.support.v4.view.PagerAdapter;
import android.support.v4.view.ViewPager;
import android.view.KeyEvent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.SeekBar;
import android.widget.SeekBar.OnSeekBarChangeListener;

import com.carboy.launch.R;
import com.carboy.launch.activity.column.BaseActivity;
import com.carboy.launch.utils.AppManager;
import com.carboy.launch.utils.Tool;
import com.carboy.launch.widgets.CustomToast;

/**
 * 设置首页Activity
 */
public class SettingMainActivity extends BaseActivity implements
		OnClickListener {
	private AudioManager mAudioManager;
	// 当前上下文
	private Context mContext;
	private Dialog voiceDialog; // 声音操作Dialog
	private int mVolume;

	// ================== 控件View =================
	private Button set_btn_wifi, set_btn_wifihot, set_btn_display,
			set_btn_datatime, set_btn_about, set_btn_apply;

	// ===================== 控件View ============================
	private RadioGroup radioGroup;
	/** 指示图按钮数组 */
	private RadioButton[] radioButtons_point;// 不需要在布局文件定义，会自动添加
	private ArrayList<View> viewList = null;
	private ViewPager viewPager;

	// ================== 其他对象 =================

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_setting_main2);
		mContext = SettingMainActivity.this;
		initViews();
		initStatusBar();
		initListeners();
		AppManager.getInstance().addActivity(this);
	}

	/**
	 * 初始化控件
	 */
	private void initViews() {

		set_btn_wifi = (Button) findViewById(R.id.setting_btn_wifi);
		set_btn_wifihot = (Button) findViewById(R.id.setting_btn_wifihot);
		set_btn_display = (Button) findViewById(R.id.setting_btn_display);
		set_btn_datatime = (Button) findViewById(R.id.setting_btn_datatime);
		set_btn_apply = (Button) findViewById(R.id.setting_btn_apply);
		set_btn_about = (Button) findViewById(R.id.setting_btn_about);

	}

	/**
	 * 初始化事件
	 */
	private void initListeners() {
		set_btn_wifi.setOnClickListener(this);
		set_btn_wifihot.setOnClickListener(this);
		set_btn_display.setOnClickListener(this);
		set_btn_datatime.setOnClickListener(this);
		set_btn_apply.setOnClickListener(this);
		set_btn_about.setOnClickListener(this);

	}

	/**
	 * 点击事件
	 */
	@Override
	public void onClick(View v) {
		Intent intent = null;
		switch (v.getId()) {
		case R.id.setting_btn_wifi:
			intent = new Intent(mContext, SWifiSettingActivity.class);
			startActivity(intent);

			break;
		case R.id.setting_btn_wifihot:
			intent = new Intent(mContext, SHotSpotActivity.class);
			startActivity(intent);
			break;
		case R.id.setting_btn_display:
			intent = new Intent(mContext, SDisplaySettingActivity.class);
			startActivity(intent);

			break;
		case R.id.setting_btn_datatime:
			String ACTION_TIME_DATE = "android.settings.DATE_SETTINGS";
			intent = new Intent(ACTION_TIME_DATE);
			intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
			startActivity(intent);
			break;
		case R.id.setting_btn_apply:
			String actionApps = "android.settings.APPLICATION_SETTINGS";
			Intent applyIntent = new Intent(actionApps);
			applyIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
			startActivity(applyIntent);
			break;
		// 流量
		case R.id.setting_btn_about:
			try {
				Tool.startApp(mContext, "com.android.settings",
						"com.android.settings.Settings$DataUsageSummaryActivity");
			} catch (Exception e) {
				// TODO: handle exception
				CustomToast.showToast(mContext, R.string.intnet_apply_error);
			}
			break;

		}
	}

	/**
	 * 重写返回键
	 */
	public boolean onKeyDown(int keyCode, KeyEvent event) {
		if (keyCode == KeyEvent.KEYCODE_BACK) {
			SettingMainActivity.this.finish();
			return true;
		}
		return super.onKeyDown(keyCode, event);
	}

	@Override
	protected void onStart() {
		super.onStart();
	}

	@Override
	protected void onRestart() {
		super.onRestart();
	}

	@Override
	protected void onStop() {
		super.onStop();
	}

	@Override
	protected void onPause() {
		super.onPause();

	}

	@Override
	protected void onResume() {
		super.onResume();

	}

	@Override
	protected void onDestroy() {
		super.onDestroy();
	}

	@Override
	public void onSaveInstanceState(Bundle outState) {
		super.onSaveInstanceState(outState);
		// Activity被回收,则保存数据
	}

	@Override
	public void onConfigurationChanged(Configuration newConfig) {
		super.onConfigurationChanged(newConfig);
		// 切屏触发
	}

	private AudioManager getAudioManager() {
		if (mAudioManager == null) {
			mAudioManager = (AudioManager) getSystemService(Context.AUDIO_SERVICE);
		}
		return mAudioManager;
	}

	/**
	 * 打开声音对话框
	 */
	private void showVoiceDialog() {
		if (voiceDialog == null) {

			AlertDialog.Builder builder = new AlertDialog.Builder(mContext);
			// 使用外部自定义布局时，加载外部xml
			LayoutInflater inflater = LayoutInflater.from(mContext);
			View view = inflater.inflate(R.layout.dialog_voice, null);
			SeekBar seekBar = (SeekBar) view.findViewById(R.id.voice_seekbar);
			int mVolume = getAudioManager().getStreamVolume(
					AudioManager.STREAM_MUSIC); // 获取当前音乐音量

			seekBar.setMax(getAudioManager().getStreamMaxVolume(
					AudioManager.STREAM_MUSIC)); // SEEKBAR设置为音量的最大阶数
			seekBar.setProgress(mVolume); // 设置seekbar为当前音量进度
			seekBar.setOnSeekBarChangeListener(new OnSeekBarChangeListener() {
				@Override
				public void onStopTrackingTouch(SeekBar seekBar) {
					// 该方法拖动进度条开始拖动的时候调用。

				}

				@Override
				public void onStartTrackingTouch(SeekBar seekBar) {
					// 该方法拖动进度条停止拖动的时候调用

				}

				@Override
				public void onProgressChanged(SeekBar seekBar, int progress,
						boolean fromUser) {
					// 该方法拖动进度条进度改变的时候调用

					getAudioManager().setStreamVolume(
							AudioManager.STREAM_MUSIC, progress, 0); // 拖动seekbar时改变音量
				}
			});
			builder.setView(view);
			voiceDialog = builder.create();
		}
		voiceDialog.show();
	}

	private class MainPageAdapter extends PagerAdapter {

		private List<View> views;

		public MainPageAdapter(List<View> views) {
			this.views = views;
		}

		@Override
		public int getCount() {
			return views.size();
		}

		@Override
		public boolean isViewFromObject(View arg0, Object arg1) {
			return arg0 == arg1;
		}

		@Override
		public void destroyItem(ViewGroup container, int position, Object object) {
			((ViewPager) container).removeView(views.get(position));
		}

		public Object instantiateItem(View v, int pos) {
			((ViewPager) v).addView(views.get(pos));
			return views.get(pos);
		}

		@Override
		public Parcelable saveState() {
			return null;
		}

	}

}
